<template>
    <div class="page" :loading="true"></div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import request from '@/request';
import { ElMessage } from 'element-plus';
const router = useRouter();
const route = useRoute();

const type = route.query.type;
const auth_code = route.query.auth_code;
const ticket = route.query.ticket;

// 获取信息
onMounted(() => {
    switch (type) {
        case 'pub':
            // 公众号授权
            if (auth_code) {
                request('miniappAuthServ', {
                    authCode: auth_code,
                    isMiniApp: 2
                }).then(res => {
                    request('miniappMultiAuthLinkServ', {
                        callbackFrontUrl: 'https://' + window.location.host + '/mp/redirect?type=ticket'
                    }).then(res => {
                        const url = res.data.url;
                        if (url) {
                            window.location = url;
                        }
                    });
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: '获取code失败!'
                });
            }

            break;
        case 'ticket':
            // 注册小程序
            if (ticket) {
                request('miniappMultiAuthBuildMpServ', {
                    ticket: ticket
                }).then(res => {
                    router.replace('/mp/info');
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: '获取ticket失败!'
                });
            }
            break;
        case 'mp':
            // 小程序授权
            if (auth_code) {
                request('miniappAuthServ', {
                    authCode: auth_code,
                    isMiniApp: 1
                }).then(res => {
                    ElMessage({
                        type: 'success',
                        message: '小程序授权成功!'
                    });
                    router.replace('/mp/info');
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: '获取code失败!'
                });
            }
            break;
        default:
            ElMessage({
                type: 'error',
                message: '页面参数错误!'
            });
            break;
    }
});
</script>
<style lang="scss" scoped>
.page {
}
</style>
